package com.xpf.controller;

import com.xpf.pojo.bo.ShopcartBO;
import com.xpf.utils.JSONResult;
import com.xpf.utils.JsonUtils;
import com.xpf.utils.RedisOperator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Api(value = "购物车接口controller", tags = {"购物车接口相关的api"})
@RequestMapping("shopcart")
@RestController //所有请求都是json对象
public class ShopcartController extends BaseController{
    
    @Autowired
    private RedisOperator redisOperator;

    @ApiOperation(value = "添加商品到购物车", notes = "添加商品到购物车", httpMethod = "POST")
    @PostMapping ("/add")
    public JSONResult add(@RequestParam String userId,
                            @RequestBody ShopcartBO shopcartBO,
                            HttpServletRequest request,
                            HttpServletResponse response){
        if (StringUtils.isBlank(userId)){
            return JSONResult.errorMsg("用户id为空");
        }

        System.out.println("shopcartBO数据："+shopcartBO);

        // 前端用户在登录的情况下，添加商品到购物车，会同时在后端同步购物车到redis缓存
        // 判断当前购物车中包含已经存在的商品，如果存在则累加购买数量
        //判断redis中是否已有商品
        String shopcarJson = redisOperator.get(FOODIE_SHOPCART+ ":" + userId);

        List<ShopcartBO> shopcartList = null;
        if (StringUtils.isNotBlank(shopcarJson)){
            //redis购物车中数据不为空，需要判断是否是同一规格商品，如果是进行数量累加
            shopcartList = JsonUtils.jsonToList(shopcarJson, ShopcartBO.class);
            boolean isHaving = false;
            for (ShopcartBO sc : shopcartList) {
                String specId = sc.getSpecId();
                if (specId.equals(shopcartBO.getSpecId())){
                    sc.setBuyCounts(sc.getBuyCounts() + shopcartBO.getBuyCounts());
                    isHaving = true;
                }
            }
            //不是同一规格商品，则直接加入购物车redis中
            if (!isHaving){
                shopcartList.add(shopcartBO);
            }

        }else {
            //redis中购物车数据为空
            shopcartList = new ArrayList<>();
            shopcartList.add(shopcartBO);
        }

        // 新增或者覆盖现有的redis中的购物车
        redisOperator.set(FOODIE_SHOPCART+ ":" + userId, JsonUtils.objectToJson(shopcartList));
        return JSONResult.ok();
    }

    @ApiOperation(value = "从购物车中删除商品", notes = "从购物车中删除商品")
    @PostMapping ("/del")
    public JSONResult del(@RequestParam String userId,
                          @RequestParam String itemSpecId,
                          HttpServletRequest request,
                          HttpServletResponse response){
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(itemSpecId)){
            return JSONResult.errorMsg("参数不能为空");
        }

        // 用户在页面删除购物车中的商品数据，如果此时用户已经登录，则需要同步删除后端购物车中的商品
        String shopcarJson = redisOperator.get(FOODIE_SHOPCART+ ":" + userId);

        if (StringUtils.isNotBlank(shopcarJson)){
            List<ShopcartBO> shopcartList = JsonUtils.jsonToList(shopcarJson, ShopcartBO.class);

            for (ShopcartBO sc : shopcartList) {
                String specId = sc.getSpecId();
                if (specId.equals(itemSpecId)){
                    shopcartList.remove(sc);
                    break;
                }
            }
            redisOperator.set(FOODIE_SHOPCART+ ":" + userId, JsonUtils.objectToJson(shopcartList));
        }

        return JSONResult.ok();
    }
}
